# -*- coding:UTF-8 -*-
import random
import threading
import time

from spider_xue.account import set_account_is_not
from spider_xue.logger import logging
from spider_xue.get_day import do_day, do_day_set_all
from spider_xue.get_five import do_five
from spider_xue.get_one import do_together_one_mintue, fix_one_mintue
from spider_xue.settings import INFO, eflag


class putIPThread(threading.Thread):  # 继承父类threading.Thread
    def __init__(self, threadID, name, workQueueIP, new_ip_db, new_ip_txt_db, dirty_ip_db, flagQueue):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.db = new_ip_db
        self.db_txt = new_ip_txt_db
        self.db_dirty = dirty_ip_db
        self.workQueueIP = workQueueIP
        self.flagQueue = flagQueue
        # self.llock = llock

    def run(self):
        logging.info(f"put_func starting {self.threadID} - {self.name}")
        put_ip_func(self.threadID, self.name, self.workQueueIP,
                    self.db, self.db_txt, self.db_dirty, self.flagQueue)


def put_ip_func(threadID, threadName, workQueue, new_ip_db, new_ip_txt_db, dirty_ip_db, flagQueue):
    while True and flagQueue.empty():
        if workQueue.qsize() < 80:
            for i in new_ip_db.find({"status": 1}):
                workQueue.put(i)
            for i in new_ip_txt_db.find({"status": 1}):
                workQueue.put(i)
            for i in dirty_ip_db.find({"status": 1}):
                workQueue.put(i)
        else:
            time.sleep(1)


class missionThread (threading.Thread):  # 继承父类threading.Thread
    def __init__(self, threadID, account, workQueue, workQueueIP, func):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = account["phone"]
        self.workQueue = workQueue
        self.workQueueIP = workQueueIP
        self.account = account
        self.func = func

    def run(self):  # 把要执行的代码写到run函数里面 线程在创建后会直接运行run函数
        logging.info(
            f"start threadName {self.name} - threadID {self.threadID}")
        self.func(self.threadID, self.name, self.account,
                  self.workQueue, self.workQueueIP)
        logging.info(
            f"Exiting threadName {self.name} - threadID {self.threadID}")


# 每2分钟   1 分钟 k线处理 以及 发送消息
def mission_together_one_mintue(threadID, threadName, account, workQueue, workQueueIP):
    countz = 0
    while True:
        # print(f'{countz} {threadID}', countz > 3, 111111111111)
        if countz > 5 and workQueue.empty():
            set_account_is_not(account)
            break
        countz += 1
        if not workQueue.empty():
            stocks = workQueue.get(True)
            ret, ip = do_together_one_mintue(account, stocks, workQueueIP)
            if ret:
                workQueue.put(ret)
            if not ret:
                logging.info(
                    f"each mission {threadID} - {threadName} all {len(stocks)} finish {len(stocks) - len(ret)} workQueue {workQueue.qsize()} IP: {ip}")
        else:
            logging.info(
                f"each wating{threadID} - {threadName} 等待抢 - 当前的workQueue {workQueue.qsize()}")
        time.sleep(random.random() + 1)


def mission_five(threadID, threadName, account, workQueue, workQueueIP):
    count = 0
    while True:
        if count > 5 and workQueue.empty():
            set_account_is_not(account)
            # send_msg("### count 30 超时了！！！！！！！！！！！！！")
            break
        if not workQueue.empty():
            stocks = workQueue.get(True)
            ret = do_five(account, stocks, workQueueIP)
            if ret:
                workQueue.put(ret)
            logging.info(
                f"each mission {threadID} - {threadName} all {len(stocks)} finish {len(stocks) - len(ret)} workQueue {workQueue.qsize()}")
        else:
            logging.info(
                f"each wating {threadID} - {threadName} 等待抢 - 当前的workQueue {workQueue.qsize()}")
        time.sleep(random.random() + 1)
        count += 1


def mission_day(threadID, threadName, account, workQueue, workQueueIP):
    count = 0
    while True:
        if count > 3 and workQueue.empty():
            set_account_is_not(account)
            # send_msg("### count 30 超时了！！！！！！！！！！！！！")
            break
        if not workQueue.empty():
            stocks = workQueue.get(True)
            ret = do_day(account, stocks, workQueueIP)
            if ret:
                workQueue.put(ret)
            logging.info(
                f"each mission {threadID} - {threadName} all {len(stocks)} finish {len(stocks) - len(ret)} workQueue {workQueue.qsize()}")
        else:
            logging.info(
                f"each wating {threadID} - {threadName} 等待抢 - 当前的workQueue {workQueue.qsize()}")
        time.sleep(random.random() + 1)
        count += 1


def mission_day_all_stock(threadID, threadName, account, workQueue, workQueueIP):
    count = 0
    while True:
        if count > 5 and workQueue.empty():
            set_account_is_not(account)
            # send_msg("### count 30 超时了！！！！！！！！！！！！！")
            break
        if not workQueue.empty():
            stocks = workQueue.get(True)
            ret = do_day_set_all(account, stocks, workQueueIP)
            if ret:
                workQueue.put(ret)
            logging.info(
                f"each mission {threadID} - {threadName} all {len(stocks)} finish {len(stocks) - len(ret)} workQueue {workQueue.qsize()}")
        else:
            logging.info(
                f"each wating {threadID} - {threadName} 等待抢 - 当前的workQueue {workQueue.qsize()}")
        time.sleep(random.random() + 1)
        count += 1


# 每3分钟   1 分钟 k线处理 以及 发送消息
def mission_fix_one_mintue(threadID, threadName, account, workQueue, workQueueIP):
    countz = 0
    while True:
        # print(f'{countz} {threadID}', countz > 3, 111111111111)
        if countz > 5 and workQueue.empty():
            set_account_is_not(account)
            break
        countz += 1
        if not workQueue.empty():
            stocks = workQueue.get(True)
            ret, ip = fix_one_mintue(account, stocks, workQueueIP)
            if ret:
                workQueue.put(ret)
            logging.info(f"each mission {threadID} - {threadName} all {len(stocks)} finish {len(stocks) - len(ret)} workQueue {workQueue.qsize()} IP: {ip}")
        else:
            logging.info(f"each wating{threadID} - {threadName} 等待抢 - 当前的workQueue {workQueue.qsize()}")
        time.sleep(random.random() + 1)
